package org.box.metadata.shell;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.box.metadata.cli.annotation.CLIConfiguration;
import org.box.metadata.cli.annotation.Option;
import org.box.metadata.shell.web.ConcurrentSessionPolicy;

@CLIConfiguration
public class WebShellDefaultConfiguration {

	private static final Logger LOG = Logger.getLogger(WebShellDefaultConfiguration.class.getName());

	private static final int DEFAULT_SERVER_PORT = 9999;
	private static final int DEFAULT_BUFFER_SIZE = 1024;

	@Option(shortName = "p", fullName = "port", 
			description = "port to run server on (9999 by default)", hasArguments = true)
	String port = "" + DEFAULT_SERVER_PORT;

	
	@Option(shortName = "s", fullName = "buffer-size", 
			description = "size of output buffer", hasArguments = true)
	String bufferSize = "" + DEFAULT_BUFFER_SIZE;
	
	@Option(shortName = "rb", fullName = "resource-base", 
			description = "the root folder of the webapp to deploy", hasArguments = true)
	String resourceBase;
	
	@Option(shortName = "w", fullName = "war-file", 
			description = "the path and name of WAR file to deploy", hasArguments = true)
	String warFile = "web-cli-webconsole.war";
	
	@Option(shortName = "csp", fullName = "concurrent-sessions-policy", 
			description = "specify the policy for concurrent sessions", hasArguments = true)
	String csp = "css";
	
	public int getPort() {
		try {
			return Integer.parseInt(port);
		} catch (NumberFormatException e) {
			if (LOG.isLoggable(Level.WARNING))
				LOG.warning("Unable to parse Port value:" + port);
			return DEFAULT_SERVER_PORT;
		}
	}
	
	public int getBufferSize() {
		try {
			return Integer.parseInt(bufferSize);
		} catch (NumberFormatException e) {
			if (LOG.isLoggable(Level.WARNING))
				LOG.warning("Unable to parse Buffer Size value:" + bufferSize);
			return DEFAULT_BUFFER_SIZE;
		}
	}
	
	public String getResourceBase() {
		return resourceBase;
	}

	public String getWarFile() {
		return warFile;
	}

	public ConcurrentSessionPolicy getCSP() {
		ConcurrentSessionPolicy v = ConcurrentSessionPolicy.findByNames(csp);
		if (v == null) {
			if (LOG.isLoggable(Level.WARNING))
				LOG.warning("Unable to parse Concurrent-Sessions-Policy value:" + csp);
			return ConcurrentSessionPolicy.CONCURRENT_SESSIONS;
		}
		return v;
	}
	
}
